package com.google.code.nexttraffic.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.google.code.nexttraffic.dao.impl.TerminalDaoJdbcImpl;
import com.google.code.nexttraffic.service.TerminalService;
import com.google.code.nexttraffic.web.model.Terminal;
import com.google.code.nexttraffic.web.model.Terminal_Temp;

public class TerminalServiceImpl implements TerminalService {

	private static TerminalDaoJdbcImpl terminalDaoJdbcImpl = null;
	private static TerminalServiceImpl terminalServiceImpl = null;

	private TerminalServiceImpl() {
	};

	public static TerminalServiceImpl getInstance() {
		if (terminalServiceImpl == null) {
			terminalDaoJdbcImpl = TerminalDaoJdbcImpl.getInstance();
			terminalServiceImpl = new TerminalServiceImpl();
		}
		return terminalServiceImpl;
	}

	@Override
	public boolean insertTerminal(Terminal terminal) {

		terminalDaoJdbcImpl.insertTerminal(terminal);

		return false;
	}

	@Override
	public List<Terminal> getListTerminal() {

		return terminalDaoJdbcImpl.getListTerminal();

	}
	
	@Override 
	public Terminal getTerminalDetailInfo(String terminalName) {
		
		Terminal t=terminalDaoJdbcImpl.getTerminalDetailInfo(terminalName);

		return t; 
	}
	public String getTimeTable(String terminalNumber){
		
		return terminalDaoJdbcImpl.getTimeTable(terminalNumber);
	}
 
	@Override
	public List<Terminal_Temp> search(Terminal_Temp temp) {

		Integer startTerminal = temp.getStartTerminal();
		Integer endTerminal = temp.getEndTerminal();
		String divsion = temp.getDivsion();

		List<Terminal_Temp> tempTerminal = null;

		if (divsion != null && divsion.equals("시외")) {

			temp.setStartTerminal(1);
			temp.setEndTerminal(8);
			temp.setDivsion("시외");

			List<Terminal_Temp> tempTerminal1 = terminalDaoJdbcImpl
					.search(temp);

			temp.setStartTerminal(2);
			temp.setEndTerminal(8);
			temp.setDivsion("시외");

			List<Terminal_Temp> tempTerminal2 = terminalDaoJdbcImpl
					.search(temp);

			tempTerminal = new ArrayList<Terminal_Temp>();

			for (Terminal_Temp temp1 : tempTerminal1) {

				List<Terminal> terminals = new ArrayList<Terminal>();

				List<String> timeTables = new ArrayList<String>();

				String linkTerminal = temp1.getLinkTerminal();

				String timeTable = temp1.getLinkTimetable();

				StringTokenizer st = new StringTokenizer(linkTerminal, ",");

				while (st.hasMoreTokens()) {

					Integer link = Integer.parseInt(st.nextToken());

					Terminal terminal = terminalDaoJdbcImpl.getTerminal(link);

					terminals.add(terminal);

				}

				st = new StringTokenizer(timeTable, ",");

				while (st.hasMoreTokens()) {

					timeTables.add(st.nextToken());

				}

				temp1.setTerminals(terminals);
				temp1.setTimetables(timeTables);

				tempTerminal.add(temp1);

			}

			for (Terminal_Temp temp1 : tempTerminal2) {

				List<Terminal> terminals = new ArrayList<Terminal>();

				List<String> timeTables = new ArrayList<String>();

				String linkTerminal = temp1.getLinkTerminal();

				String timeTable = temp1.getLinkTimetable();

				StringTokenizer st = new StringTokenizer(linkTerminal, ",");

				while (st.hasMoreTokens()) {

					Integer link = Integer.parseInt(st.nextToken());

					Terminal terminal = terminalDaoJdbcImpl.getTerminal(link);

					terminals.add(terminal);

				}

				st = new StringTokenizer(timeTable, ",");

				while (st.hasMoreTokens()) {

					timeTables.add(st.nextToken());

				}

				temp1.setTerminals(terminals);
				temp1.setTimetables(timeTables);

				tempTerminal.add(temp1);

			}

		} else if (divsion != null && divsion.equals("고속")) {


			temp.setStartTerminal(3);
			temp.setEndTerminal(10);
			temp.setDivsion("고속");

			tempTerminal = terminalDaoJdbcImpl.search(temp);

			for (Terminal_Temp temp1 : tempTerminal) {

				List<Terminal> terminals = new ArrayList<Terminal>();

				List<String> timeTables = new ArrayList<String>();

				String linkTerminal = temp1.getLinkTerminal();
				String timeTable = temp1.getLinkTimetable();

				StringTokenizer st = new StringTokenizer(linkTerminal, ",");

				while (st.hasMoreTokens()) {

					Integer link = Integer.parseInt(st.nextToken());

					Terminal terminal = terminalDaoJdbcImpl.getTerminal(link);

					terminals.add(terminal);

				}

				st = new StringTokenizer(timeTable, ",");

				while (st.hasMoreTokens()) {

					timeTables.add(st.nextToken());

				}

				temp1.setTerminals(terminals);
				temp1.setTimetables(timeTables);

			}

		} else {

			temp.setStartTerminal(3);
			temp.setEndTerminal(8);
			temp.setDivsion("혼합");

			tempTerminal = terminalDaoJdbcImpl.search(temp);

			for (Terminal_Temp temp1 : tempTerminal) {

				List<Terminal> terminals = new ArrayList<Terminal>();

				List<String> timeTables = new ArrayList<String>();

				String linkTerminal = temp1.getLinkTerminal();
				String timeTable = temp1.getLinkTimetable();

				StringTokenizer st = new StringTokenizer(linkTerminal, ",");

				while (st.hasMoreTokens()) {

					Integer link = Integer.parseInt(st.nextToken());

					Terminal terminal = terminalDaoJdbcImpl.getTerminal(link);

					terminals.add(terminal);

				}

				st = new StringTokenizer(timeTable, ",");

				while (st.hasMoreTokens()) {

					timeTables.add(st.nextToken());

				}

				temp1.setTerminals(terminals);
				temp1.setTimetables(timeTables);

			}

		}

		return tempTerminal;
	}

}
